using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace MonoCash.ObjectModel
{
    class Account : ObjectModel
    {

        public static Account[] GetAccountsByTypeName(string accountTypeName)
        {
            List<Account> temp = new List<Account>();
            foreach (Account a in Accounts)
            {
                if (a.AccountType.Name == accountTypeName)
                {
                    temp.Add(a);
                }
            }
            return temp.ToArray();
        }

        public static List<Account> Accounts
        {
            get
            {
                List<Account> temp = new List<Account>();
                foreach (DataRow row in Program.AppSettings.Data.Accounts.Select("", "DisplayOrder ASC"))
                {
                    temp.Add(new Account(row["ID"]));
                }
                return temp;
            }
        }
        public List<Account> AccountsForTransactions
        {
            get
            {
                List<Account> temp = new List<Account>();
                foreach (Account a in Accounts)
                {
                    if (a.AllowTransactions && a.ID != this.ID)
                    {
                        temp.Add(a);
                    }
                }
                return temp;
            }
        }
        public Account() : base() { }
        public Account(object objectId) : base(objectId)
        {
            
        }
        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; SyncToBackend(); }
        }

        private int accountTypeID = -1;
        public int AccountTypeID
        {
            get { return accountTypeID; }
            set { accountTypeID = value; SyncToBackend(); }
        }
        public AccountType AccountType
        {
            get { return new AccountType(AccountTypeID); }
            set { AccountTypeID = value.ID; }
        }

        private int accountingCategoryID = -1;
        public int AccountingCategoryID
        {
            get { return accountingCategoryID; }
            set { accountingCategoryID = value; SyncToBackend(); }
        }
	
        public AccountingCategory AccountingCategory 
        {
            get
            {
                return new AccountingCategory(AccountingCategoryID);
            }
            set
            {
                AccountingCategoryID = value.ID;
            }
        }

        private int parentAccountID = -1;

        public int ParentAccountID
        {
            get { return parentAccountID; }
            set { parentAccountID = value; SyncToBackend(); }
        }
        public Account ParentAccount
        {
            get { return new Account(ParentAccountID); }
            set { ParentAccountID = value.ID; }
        }

        private bool allowTransactions = false;
        public bool AllowTransactions
        {
            get { return allowTransactions; }
            set { allowTransactions = value; SyncToBackend(); }
        }
	

        public string LongName 
        {
            get
            {
                string temp = Name;
                try
                {
                    if (ParentAccountID != -1)
                    {
                        temp = ParentAccount.LongName + " > " + Name;
                    }
                }
                catch { }           
                return temp;
            }
        }
        public Account[] ChildAccounts
        {
            get
            {
                List<Account> children = new List<Account>();
                foreach (DataRow child in Program.AppSettings.Data.Accounts.Select("ParentAccountID=" + ID.ToString(), "DisplayOrder ASC"))
                {
                    children.Add(new Account(child["ID"]));
                }
                return children.ToArray();
            }
        }
        public List<Account> PossibleParentAccounts
        {
            get
            {
                List<Account> parents = new List<Account>();
                parents.Add(new Account());//Root level account
                parents.AddRange(Accounts);
                parents.Remove(this);
                return parents;
            }
        }
        public Decimal Balance
        {
            get
            {
                Decimal temp = 0;
                try
                {
                    Object compute = Program.AppSettings.Data.TransactionDetails.Compute("Sum(Amount)", "AccountID=" + ID.ToString());
                    if (compute.GetType().Name != "DBNull")
                    {
                        temp = Convert.ToDecimal(compute);
                    }
                }
                catch { }
                return temp;
            }
        }
        public Decimal DisplayBalance
        {
            get
            {
                Decimal temp = Balance;
                if (AccountingCategory.ReverseSignForDisplay)
                {
                    temp *= -1;
                }
                return temp;
            }
        }
        
        private int displayOrder = 0;
        public int DisplayOrder
        {
            get { return displayOrder; }
            set { displayOrder = value; SyncToBackend(); }
        }

        private bool isVisible = true;
        public bool IsVisible
        {
            get { return isVisible; }
            set { isVisible = value; SyncToBackend(); }
        }

        private TransactionDetailCollection transactionDetails;
        public TransactionDetailCollection TransactionDetails
        {
            get
            {
                if (transactionDetails == null)
                {
                    transactionDetails = new TransactionDetailCollection(this);
                }
                return transactionDetails;
            }
        }
    }
}
